package org.example.user.manage.controller;

import org.example.commons.controller.BaseController;
import org.example.commons.utils.SortUtils;
import org.example.commons.vo.ResultVO;
import org.example.user.manage.dto.SysHealthStatusDTO;
import org.example.user.manage.service.ISysHealthStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.annotation.Secured;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 健康状态 控制层
 *
 * @author bothin
 */
@Secured({"ROLE_SUPER_ADMIN","ROLE_ADMIN","ROLE_CHECK_MAN"})
@RestController
@RequestMapping("/api/health/status")
public class SysHealthStatusController extends BaseController {

    @Autowired
    private ISysHealthStatusService healthStatusService;

    /**
     * 导出excel
     * @param response
     * @throws IOException
     */
    @GetMapping("/export/excel")
    public void exportExcel(@RequestParam(name = "ids", required = false)  Long[] ids,
                            HttpServletResponse response) throws IOException {
        List<SysHealthStatusDTO> data = null;
        if (ids==null || ids.length==0){
            data = healthStatusService.queryAll();
        }else {
            data = healthStatusService.findByStatusIdIn(ids);
        }
        super.exportExcel(response,data,SysHealthStatusDTO.class,"住户健康状态数据表");
    }

    @GetMapping("/{id}")
    public ResultVO findOne(@PathVariable("id") Long id) {
        return ResultVO.success(healthStatusService.queryById(id));
    }


    /**
     * 根据住房id查询出入记录
     * @param userId 住户id
     * @return ResultVO
     */
    @GetMapping("/userId")
    public ResultVO findByUserId(@RequestParam(name = "userId") Long userId){
        return ResultVO.success(healthStatusService.findByUserIdDTO(userId));
    }


    /**
     * 查询住户列表
     *
     * @param page  页数
     * @param size  每页大小
     * @param isAsc 是否正序排序
     * @param field 排序的字段
     * @return ResultVO
     */
    @GetMapping
    public ResultVO findList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "size", defaultValue = "10") Integer size,
                             @RequestParam(name = "isAsc", defaultValue = "true") Boolean isAsc,
                             @RequestParam(name = "field", defaultValue = "userId") String field) {
        //排序
        Sort sort = SortUtils.getSort(isAsc, field);
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return ResultVO.success(healthStatusService.queryAllPage(pageRequest));
    }

    /**
     * 条件查询列表
     *
     * @param page  页数
     * @param size  每页大小
     * @param isAsc 是否正序排序
     * @param field 排序的字段
     * @return ResultVO
     */
    @GetMapping("/search")
    public ResultVO searchList(@RequestParam(name = "userName", defaultValue = "") String name,
                               @RequestParam(name = "healthStatus", required = false)  Integer healthStatus,
                               @RequestParam(name = "dates", required = false) Date[] dates,
                               @RequestParam(name = "page", defaultValue = "1") Integer page,
                               @RequestParam(name = "size", defaultValue = "10") Integer size,
                               @RequestParam(name = "isAsc", defaultValue = "true") Boolean isAsc,
                               @RequestParam(name = "field", defaultValue = "") String field) {
        PageRequest pageRequest = null;
        if (StringUtils.isEmpty(field)) {
            pageRequest = PageRequest.of(page - 1, size);
        } else {
            //排序
            Sort sort = SortUtils.getSort(isAsc, field);
            pageRequest = PageRequest.of(page - 1, size, sort);
        }

        if (dates==null || dates.length<2){
            dates = new Date[2];
        }

        Page<SysHealthStatusDTO> sysHealthStatusDTOS = healthStatusService.searchList(name, healthStatus, dates, pageRequest);
        return ResultVO.success(sysHealthStatusDTOS);
    }
    /**
     * 更新或保存健康状态
     *
     * @param healthStatusDTO 健康状态
     * @return ResultVO
     */
    @PostMapping
    public ResultVO saveSysUser(@RequestBody SysHealthStatusDTO healthStatusDTO) {
        return ResultVO.success(healthStatusService.insert(healthStatusDTO));
    }

    @DeleteMapping
    public ResultVO deleteSysUser(@RequestParam("id") Long id) {
        healthStatusService.deleteById(id);
        return ResultVO.success();
    }

    @DeleteMapping("/list")
    public ResultVO deleteSysUser(@RequestParam("ids") Long[] ids) {
        for (Long id : ids) {
            healthStatusService.deleteById(id);
        }
        return ResultVO.success();
    }


}
